package touhou;

import gameframework.game.Game;

import java.awt.Canvas;
import java.awt.Point;
import java.awt.geom.Point2D;
import touhou.entity.Blocker;
import touhou.entity.EnemyTest;
import touhou.entity.Hero;
import touhou.game.CanvasDefaultImpl;
import touhou.game.GameLevelFastImpl;
import touhou.game.GameMovableDriverDefaultImpl;
import touhou.game.GameShooter;
import touhou.game.GameShooterDriverDefaultImpl;
import touhou.game.GameUniverseShooterImpl;
import touhou.game.GameUniverseViewPortShooterImpl;
import touhou.game.MoveBlockerChecker;
import touhou.game.MoveBlockerCheckerDefaultImpl;
import touhou.game.OverlapProcessor;
import touhou.game.OverlapProcessorSimpleImpl;
import touhou.game.OverlapRulesApplier;
import touhou.rule.TouhouOverlapRules;
import touhou.base.MoveStrategyBoss1;
import touhou.base.MoveStrategyKeyboard;
import touhou.base.ShootStrategyBoss1;
import touhou.base.ShootStrategyKeyboard;

public class GameLevelTest extends GameLevelFastImpl {
	Canvas canvas;

	public GameLevelTest(Game g) {
		super(g);
		canvas = g.getCanvas();
	}

	public static final int SPRITE_SIZE = 16;
	
	@Override
	protected void init() {

		OverlapProcessor overlapProcessor = new OverlapProcessorSimpleImpl();
		MoveBlockerChecker moveBlockerChecker = createMBC();

		OverlapRulesApplier overlapRules = new TouhouOverlapRules();
		overlapProcessor.setOverlapRules(overlapRules);

		universe = new GameUniverseShooterImpl(moveBlockerChecker, overlapProcessor, canvas);
		overlapRules.setUniverse(universe);

		gameBoard = new GameUniverseViewPortShooterImpl(canvas, universe, backgroundImage());
		((CanvasDefaultImpl) canvas).setDrawingGameBoard(gameBoard);
		
		createHero(canvas, moveBlockerChecker);
		
		createBoss1(canvas);
	}
	
	private MoveBlockerChecker createMBC() {
		MoveBlockerCheckerDefaultImpl moveBlockerChecker = new MoveBlockerCheckerDefaultImpl();
		
		int height = canvas.getHeight();
		int width= canvas.getWidth();
		
		Blocker left  = new Blocker(new Point2D.Double(-60,   -50), 50, height+100);
		Blocker right = new Blocker(new Point2D.Double(width, -50), 50, height+100);
		Blocker up    = new Blocker(new Point2D.Double(-50,   -60), width+100, 50);
		Blocker down  = new Blocker(new Point2D.Double(-50,height), width+100, 50);
		
		moveBlockerChecker.addMoveBlocker(left);
		moveBlockerChecker.addMoveBlocker(right);
		moveBlockerChecker.addMoveBlocker(up);
		moveBlockerChecker.addMoveBlocker(down);
		
		return moveBlockerChecker;
	}
	
	private void createHero(Canvas canvas, MoveBlockerChecker mbc) {
		Hero myHero = new Hero(canvas);
		
		GameShooterDriverDefaultImpl heroShootDriver = new GameShooterDriverDefaultImpl(canvas,4);
		ShootStrategyKeyboard shootKeyStr = new ShootStrategyKeyboard(canvas);
		heroShootDriver.setStrategy(shootKeyStr);
		canvas.addKeyListener(shootKeyStr);
		GameShooter shooter = new GameShooter(30);
		myHero.accept(shooter);
		shooter.setDriver(heroShootDriver);
		
		universe.addGameEntity(shooter);
		
		GameMovableDriverDefaultImpl heroDriver = new GameMovableDriverDefaultImpl();
		MoveStrategyKeyboard keyStr = new MoveStrategyKeyboard();
		heroDriver.setStrategy(keyStr);
		heroDriver.setmoveBlockerChecker(mbc);
		canvas.addKeyListener(keyStr);
		myHero.setDriver(heroDriver);
		myHero.setPosition(new Point(10,10));
		
		universe.addGameEntity(myHero);
	}
	
	private void createBoss1(Canvas canvas) {
		EnemyTest et = new EnemyTest(canvas);
		
		GameShooterDriverDefaultImpl etShootDriver = new GameShooterDriverDefaultImpl(canvas);
		ShootStrategyBoss1 strategy = new ShootStrategyBoss1(canvas);
		etShootDriver.setStrategy(strategy);
		GameShooter etShooter = new GameShooter();
		et.accept(etShooter);
		etShooter.setDriver(etShootDriver);
		
		universe.addGameEntity(etShooter);
		
		GameMovableDriverDefaultImpl etMoveDriver = new GameMovableDriverDefaultImpl();
		MoveStrategyBoss1 etMoveStrategy = new MoveStrategyBoss1();
		etMoveDriver.setStrategy(etMoveStrategy);
		et.setDriver(etMoveDriver);
		et.setPosition(new Point(100,100));
		
		universe.addGameEntity(et);
	}

	private String backgroundImage() {
		return "images/background_level_test.gif";
	}

}
